package com.fangcun.controller.api.old.agent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.sys.PageBean;
import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProRunTimeException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.base.BaseController;
import com.fangcun.entity.Agent;
import com.fangcun.entity.AgentShareBenefitsRule;
import com.fangcun.service.api.AgentService;

@RestController
@RequestMapping("/api/{version}/auth/agent")
public class AgentController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(AgentController.class);

	@Autowired
	private AgentService agentService;

	@ApiVersion(1.0)
	@RequestMapping("details")
	private ResponseMsg details(Long agentId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(agentService.details(getApiUserId(), agentId));
		} catch (ProException e) {
			LOG.error("-查询该用户是否是代理出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-查询该用户是否是代理异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping("add")
	private ResponseMsg add(@RequestBody Agent request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (request.getStatus() == 4 && request.getUserId() == null) {
				throw new ProValiDataException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
			} else if (request.getStatus() != 4) {
				request.setUserId(getApiUserId());
			}
			agentService.add(request, getApiUserId());
		} catch (ProException e) {
			LOG.error("-提交申请出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-提交申请异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 查询 手机号 是否 有该用户 **/
	@ApiVersion(1.0)
	@RequestMapping("get/phone")
	private ResponseMsg getPhone(String phone) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(agentService.getPhone(phone));
		} catch (ProException e) {
			LOG.error("-查询是否有该用户出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-查询是否有该用户异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** api 修改 （拒绝 5 接收1） **/
	@ApiVersion(1.0)
	@RequestMapping("update")
	private ResponseMsg update(@RequestBody Agent request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			agentService.update(request);
		} catch (ProException e) {
			LOG.error("-查询是否有改用户出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-查询是否有改用户异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 邀请记录 求和 **/
	@ApiVersion(1.0)
	@RequestMapping("invitation")
	private ResponseMsg invitation() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(agentService.invitation(getApiUserId()));
		} catch (ProException e) {
			LOG.error("-邀请页总邀请人数出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-邀请页总邀请人数异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 邀请列表记录 **/
	@ApiVersion(1.0)
	@RequestMapping("record")
	private ResponseMsg record(@RequestBody PageBean<Agent> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(agentService.list(request));
		} catch (ProException e) {
			LOG.error("-邀请记录出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-邀请记录异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 二级代理列表 **/
	@ApiVersion(1.0)
	@RequestMapping("two/list")
	private ResponseMsg secondList(@RequestBody PageBean<Agent> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(agentService.AgentList(request));
		} catch (ProException e) {
			LOG.error("-查看二级出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-查看二级异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/profit/detail")
	private ResponseMsg agentShareBenefitsRuleDetail(Long agentId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(agentService.detail(agentId));
		} catch (ProRunTimeException e) {
			LOG.error("-代理商分利-详情出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-代理商分利-详情异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/profit/update")
	private ResponseMsg settingShareBenefits(@RequestBody AgentShareBenefitsRule request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			agentService.settingShareBenefits(request);
		} catch (ProRunTimeException e) {
			LOG.error("-代理商分利-修改出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-代理商分利-修改异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

}
